package service;

import javax.websocket.Session;
import java.util.concurrent.ConcurrentHashMap;

public class WebsocketSessionServiceImp implements WebsocketSessionService {

    private static WebsocketSessionServiceImp websocketSessionServiceImp = new WebsocketSessionServiceImp();
    public static WebsocketSessionServiceImp getWebsocketSessionServiceImp() {
        return websocketSessionServiceImp;
    }

    private ConcurrentHashMap<String, Session> sessionMap;

    private WebsocketSessionServiceImp() {
        sessionMap = new ConcurrentHashMap<>();
    }

    @Override
    public String putSession(Session session) {
        String sessionId = session.getId();
        sessionMap.put(sessionId, session);
        return sessionId;
    }

    @Override
    public Session getAndRemoveWSSession(String sessionId) {
        return sessionMap.remove(sessionId);
    }
}
